package backstage.app.controller.levea;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import backstage.app.entity.Levea;
import backstage.app.mapper.LeveaMapper;
import backstage.app.mapper.LevelkillStudentMapper;
import backstage.app.respont.RespontDateJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("${spring.application.name}/leavea/api")
@Api("leave-控制器")
public class LeveaController {
	
	
	@Autowired
	private LeveaMapper Leveamapper;
	
	@Autowired
	private LevelkillStudentMapper levelkillStudentMapper; 
	
	@PostMapping("/list")
	@ApiOperation(value="查询技能等级")
	public List<Levea>list(){
		QueryWrapper<Levea>queryWrapper=new QueryWrapper<Levea>();
		queryWrapper.select("leid","item_context_one","create_time");
		List<Levea> leaver=Leveamapper.selectList(queryWrapper);
		for (Levea levea : leaver) {
			try {
				int total=levelkillStudentMapper.selectBykillContextIdCount(levea.getLeid());
				levea.setTotal(total);
			} catch (Exception e) {
				 
			}
			 
		}
		return leaver;
	}
	
	
	@PostMapping("/insert")
	@ApiOperation(value="添加技能等级")
	public RespontDateJson<?>insert(
			 
			@RequestParam(value="itemContextOne",required = false)String itemContextOne,
			@RequestParam(value="itemContextTow",required = false)String itemContextTow,
			@RequestParam(value="itemContextThree",required = false)String itemContextThree,
			@RequestParam(value="itemContextFroth",required = false)String itemContextFroth,
			@RequestParam(value="itemContextFive",required = false)String itemContextFive,
			@RequestParam(value="itemContextSix",required = false)String itemContextSix,
			@RequestParam(value="itemContextSeven",required = true)String itemContextSeven
			){
		Levea leave=new Levea();
		 
		leave.setItemContextOne(itemContextOne);
		leave.setItemContextTow(itemContextTow);
		leave.setItemContextThree(itemContextThree);
		leave.setItemContextFroth(itemContextFroth);
		leave.setItemContextFive(itemContextFive);
		leave.setItemContextSix(itemContextSix);
		leave.setItemContextSeven(itemContextSeven);
		leave.setCreateTime(LocalDateTime.now());
		leave.setLastUpdateTime(LocalDateTime.now());
		int leaver=Leveamapper.insert(leave);
		return RespontDateJson.ok(leaver, "添加等级成功");
	}
	
	
	@PostMapping("/update")
	@ApiOperation(value="添加技能等级")
	public RespontDateJson<?>update(
			@RequestParam(value="itemContext",required = true)String itemContext,
			@RequestParam(value="itemContextOne",required = false)String itemContextOne,
			@RequestParam(value="itemContextTow",required = false)String itemContextTow,
			@RequestParam(value="itemContextThree",required = false)String itemContextThree,
			@RequestParam(value="itemContextFroth",required = false)String itemContextFroth,
			@RequestParam(value="itemContextFive",required = false)String itemContextFive,
			@RequestParam(value="itemContextSix",required = false)String itemContextSix
			){
		Levea leave=new Levea();
		leave.setItemContextSeven(itemContext);
		leave.setItemContextOne(itemContextOne);
		leave.setItemContextTow(itemContextTow);
		leave.setItemContextThree(itemContextThree);
		leave.setItemContextFroth(itemContextFroth);
		leave.setItemContextFive(itemContextFive);
		leave.setItemContextSix(itemContextSix);
		leave.setLastUpdateTime(LocalDateTime.now());
		int leaver=Leveamapper.updateByPrimaryKey(leave);
		return RespontDateJson.ok(leaver, "修改技能成功等级");
	}
	
	@GetMapping("/byid")
	@ApiOperation(value="根据id查询")
	public RespontDateJson<?>byid(
			@RequestParam(value="leid",required = true)Integer leid){
		Levea lea = null ;
		try {
			  lea=Leveamapper.selectByPrimaryKey(leid);
			  return RespontDateJson.ok(lea, "查询成功");
		} catch (Exception e) {
			 return RespontDateJson.fuall(lea, "查询失败leid="+leid);
		}
		 
	}
	
}
